#include "http_my.h"

/**
 @brief GET测试接口 --- GET
 @param pResult -[out] 返回json数据
 @return 无
*/
void queryClientInfo(cJSON *pResult)
{
    char sendData[RESPONSE_DATA_SIZE] = {0};
    cJSON_AddStringToObject(pResult, "data", "Hello ESP8266");
    sendGetResponse(true, sendData, pResult);
}

/**
 @brief POST测试接口 --- POST
 @param pRequestData -[in] 请求的数据
 @param pResult -[out] 返回json数据
 @return 无
*/
void configWifi(char *pRequestData, cJSON *pResult)
{
    cJSON *pRoot = cJSON_Parse(pRequestData);
    if (!pRoot)
    {
        return;
    }

    char sendData[RESPONSE_DATA_SIZE] = {0};
    cJSON *pRequest = cJSON_GetObjectItem(pRoot, "request"); // 解析request字段内容
    if (!pRequest)
    {
        cJSON_AddStringToObject(pResult, "info", "No request Item");
        sendPostResponse(false, sendData, pResult);
        cJSON_Delete(pRoot);
        return;
    }

    cJSON *pStation = cJSON_GetObjectItem(pRequest, "station"); // 解析request子节点station字段内容
    if (pStation)
    {
        cJSON *pcmd = cJSON_GetObjectItem(pStation, "cmd");
        cJSON *pmsg = cJSON_GetObjectItem(pStation, "msg");
        if (!pcmd)
        {
            cJSON_AddStringToObject(pResult, "info", "No cmd Item");
            sendPostResponse(false, sendData, pResult);
            cJSON_Delete(pRoot);
            return;
        }
        if (!pmsg)
        {
            cJSON_AddStringToObject(pResult, "info", "No msg Item");
            sendPostResponse(false, sendData, pResult);
            cJSON_Delete(pRoot);
            return;
        }

        printf("cmd : %s \n", pcmd->valuestring);
        printf("msg : %s \n", pmsg->valuestring);

        /* 处理区域 */
        /* 测试代码 */
        xTaskCreate(uart_event_task, "uart_event_task", 2048, NULL, 12, NULL);

        int countDown = 1000;
        while (countDown > 0 && !state)
        {
            for (int i = 0; i < 5; i++)
            {
                os_delay_us(1000);
            }
            countDown--;
        }

        uart_write_bytes(uart_log_port, (const char *)msg, size);

        state = 0;

        if (countDown <= 0)
        {
            cJSON_AddStringToObject(pResult, "info", "Error ! time out");
            sendPostResponse(false, sendData, pResult);
            cJSON_Delete(pRoot);
            return;
        }
        else
        {
            cJSON_AddStringToObject(pResult, "info", "Massage receive succeed");
            sendPostResponse(true, sendData, pResult);
            cJSON_Delete(pRoot);
            return;
        }
        /* 测试结束 */
    }
    else
    {
        cJSON_AddStringToObject(pResult, "info", "No Request Item");
        sendPostResponse(false, sendData, pResult);
    }

    cJSON_Delete(pRoot);
}

/**
 @brief 用户信息获取接口 --- POST
 @param pRequestData -[in] 请求的数据
 @param pResult -[out] 返回json数据
 @return 无
*/
void configUserInfo(char *pRequestData, cJSON *pResult)
{
    cJSON *pRoot = cJSON_Parse(pRequestData);
    if (!pRoot)
    {
        return;
    }

    char sendData[RESPONSE_DATA_SIZE] = {0};
    cJSON *pRequest = cJSON_GetObjectItem(pRoot, "request"); // 解析request字段内容
    if (!pRequest)
    {
        cJSON_AddStringToObject(pResult, "info", "No request Item");
        sendPostResponse(false, sendData, pResult);
        cJSON_Delete(pRoot);
        return;
    }

    cJSON *pStation = cJSON_GetObjectItem(pRequest, "station"); // 解析request子节点station字段内容
    if (pStation)
    {
        cJSON *pusername = cJSON_GetObjectItem(pStation, "username");
        cJSON *ppassword = cJSON_GetObjectItem(pStation, "password");
        if (!pusername)
        {
            cJSON_AddStringToObject(pResult, "info", "No username Item");
            sendPostResponse(false, sendData, pResult);
            cJSON_Delete(pRoot);
            return;
        }
        if (!ppassword)
        {
            cJSON_AddStringToObject(pResult, "info", "No password Item");
            sendPostResponse(false, sendData, pResult);
            cJSON_Delete(pRoot);
            return;
        }

        printf("username : %s \n", pusername->valuestring);
        printf("password : %s \n", ppassword->valuestring);

        /* 验证用户名和密码 */
        if (strcmp(pusername->valuestring, USER_NAME) == 0 && strcmp(ppassword->valuestring, USER_PASSWORD) == 0)
        {
            cJSON_AddStringToObject(pResult, "info", "Password correct");

            cJSON *pUserinfo = cJSON_CreateObject();
            cJSON_AddStringToObject(pUserinfo, "username", USER_NAME);
            cJSON_AddStringToObject(pUserinfo, "password", USER_PASSWORD);
            cJSON_AddStringToObject(pUserinfo, "userid", USER_ID);
            cJSON_AddStringToObject(pUserinfo, "phonenumber", PHONE_NUMBER);

            cJSON_AddItemToObject(pResult, "userinfo", pUserinfo);
        }
        else
        {
            cJSON_AddStringToObject(pResult, "info", "User name or Password incorrect");
        }

        sendPostResponse(true, sendData, pResult);
    }
    else
    {
        cJSON_AddStringToObject(pResult, "info", "No Request Item");
        sendPostResponse(false, sendData, pResult);
    }

    cJSON_Delete(pRoot);
}

/**
 @brief 设备信息获取接口 --- POST
 @param pRequestData -[in] 请求的数据
 @param pResult -[out] 返回json数据
 @return 无
*/
void configDeviceInfo(char *pRequestData, cJSON *pResult)
{
    cJSON *pRoot = cJSON_Parse(pRequestData);
    if (!pRoot)
    {
        return;
    }

    char sendData[RESPONSE_DATA_SIZE] = {0};
    cJSON *pRequest = cJSON_GetObjectItem(pRoot, "request"); // 解析request字段内容
    if (!pRequest)
    {
        cJSON_AddStringToObject(pResult, "info", "No request Item");
        sendPostResponse(false, sendData, pResult);
        cJSON_Delete(pRoot);
        return;
    }

    cJSON *pStation = cJSON_GetObjectItem(pRequest, "station"); // 解析request子节点station字段内容
    if (pStation)
    {
        cJSON *pusername = cJSON_GetObjectItem(pStation, "username");
        cJSON *ppassword = cJSON_GetObjectItem(pStation, "password");
        if (!pusername)
        {
            cJSON_AddStringToObject(pResult, "info", "No username Item");
            sendPostResponse(false, sendData, pResult);
            cJSON_Delete(pRoot);
            return;
        }
        if (!ppassword)
        {
            cJSON_AddStringToObject(pResult, "info", "No password Item");
            sendPostResponse(false, sendData, pResult);
            cJSON_Delete(pRoot);
            return;
        }

        printf("username : %s \n", pusername->valuestring);
        printf("password : %s \n", ppassword->valuestring);

        /* 验证用户名和密码 */
        if (strcmp(pusername->valuestring, USER_NAME) == 0 && strcmp(ppassword->valuestring, USER_PASSWORD) == 0)
        {
            cJSON_AddStringToObject(pResult, "info", "Password correct");

            cJSON *pDeviceinfo = cJSON_CreateObject();
            cJSON_AddStringToObject(pDeviceinfo, "deviceid", DEVICE_ID);

            cJSON_AddItemToObject(pResult, "deviceinfo", pDeviceinfo);
        }
        else
        {
            cJSON_AddStringToObject(pResult, "info", "User name or Password incorrect");
        }

        sendPostResponse(true, sendData, pResult);
    }
    else
    {
        cJSON_AddStringToObject(pResult, "info", "No Request Item");
        sendPostResponse(false, sendData, pResult);
    }

    cJSON_Delete(pRoot);
}

/**
 @brief DHT信息获取接口 --- POST
 @param pRequestData -[in] 请求的数据
 @param pResult -[out] 返回json数据
 @return 无
*/
void configDHTInfo(char *pRequestData, cJSON *pResult)
{
    cJSON *pRoot = cJSON_Parse(pRequestData);
    if (!pRoot)
    {
        return;
    }

    char sendData[RESPONSE_DATA_SIZE] = {0};
    cJSON *pRequest = cJSON_GetObjectItem(pRoot, "request"); // 解析request字段内容
    if (!pRequest)
    {
        cJSON_AddStringToObject(pResult, "info", "No request Item");
        sendPostResponse(false, sendData, pResult);
        cJSON_Delete(pRoot);
        return;
    }

    cJSON *pStation = cJSON_GetObjectItem(pRequest, "station"); // 解析request子节点station字段内容
    if (pStation)
    {
        cJSON *pusername = cJSON_GetObjectItem(pStation, "username");
        cJSON *ppassword = cJSON_GetObjectItem(pStation, "password");
        cJSON *pmodule = cJSON_GetObjectItem(pStation, "module");

        if (!pusername)
        {
            cJSON_AddStringToObject(pResult, "info", "No username Item");
            sendPostResponse(false, sendData, pResult);
            cJSON_Delete(pRoot);
            return;
        }
        if (!ppassword)
        {
            cJSON_AddStringToObject(pResult, "info", "No password Item");
            sendPostResponse(false, sendData, pResult);
            cJSON_Delete(pRoot);
            return;
        }
        if (pmodule)
        {
            cJSON *pmodulename = cJSON_GetObjectItem(pmodule, "modulename");
            cJSON *pmoduleparam = cJSON_GetObjectItem(pmodule, "moduleparam");

            if (!pmodulename)
            {
                cJSON_AddStringToObject(pResult, "info", "No moduleName Item");
                sendPostResponse(false, sendData, pResult);
                cJSON_Delete(pRoot);
                return;
            }

            printf("username : %s \n", pusername->valuestring);
            printf("password : %s \n", ppassword->valuestring);
            printf("modulename : %s \n", pmodulename->valuestring);

            if (pmoduleparam)
            {
                printf("moduleparam : %s \n", pmoduleparam->valuestring);
            }

            /* 验证用户名和密码 */
            if (strcmp(pusername->valuestring, USER_NAME) == 0 && strcmp(ppassword->valuestring, USER_PASSWORD) == 0)
            {
                cJSON_AddStringToObject(pResult, "info", "Password correct");

                cJSON *pReturninfo = cJSON_CreateObject();

                cJSON_AddItemToObject(pResult, "returninfo", pReturninfo);

                /* 指令发送区域 */
                /* 不带参数区域 */
                /* DHT指令 */
                if (strcmp(pmodulename->valuestring, "DHT") == 0)
                {
                    /* 发送串口指令 */
                    uart_write_bytes(uart_log_port, "DHTRequest", strlen("DHTRequest"));
                    oled_http_request("DHTRequest");
                }
                if (strcmp(pmodulename->valuestring, "REED") == 0)
                {
                    /* 发送串口指令 */
                    uart_write_bytes(uart_log_port, "REEDRequest", strlen("REEDRequest"));
                    oled_http_request("REEDRequest");
                }
                if (strcmp(pmodulename->valuestring, "PRESSURE") == 0)
                {
                    /* 发送串口指令 */
                    uart_write_bytes(uart_log_port, "PRESSURERequest", strlen("PRESSURERequest"));
                    oled_http_request("PRESSURERequest");
                }
                if (strcmp(pmodulename->valuestring, "SAFETYCHECK") == 0)
                {
                    /* 发送串口指令 */
                    uart_write_bytes(uart_log_port, "SAFETYCHECKRequest", strlen("SAFETYCHECKRequest"));
                    oled_http_request("SAFETYCHECKRequest");
                }

                /* 带参数区域 */
                /* STEERA指令 */
                if (strcmp(pmodulename->valuestring, "STEERA") == 0)
                {
                    char send[100] = "STEERARequest=";
                    strcat(send, pmoduleparam->valuestring);
                    /* 发送串口指令 */
                    uart_write_bytes(uart_log_port, send, strlen(send));
                    oled_http_request(send);
                }
                if (strcmp(pmodulename->valuestring, "STEERB") == 0)
                {
                    char send[100] = "STEERBRequest=";
                    strcat(send, pmoduleparam->valuestring);
                    /* 发送串口指令 */
                    uart_write_bytes(uart_log_port, send, strlen(send));
                    oled_http_request(send);
                }
                if (strcmp(pmodulename->valuestring, "SAFETY") == 0)
                {
                    char send[100] = "SAFETYRequest=";
                    strcat(send, pmoduleparam->valuestring);
                    /* 发送串口指令 */
                    uart_write_bytes(uart_log_port, send, strlen(send));
                    oled_http_request(send);
                }
                if (strcmp(pmodulename->valuestring, "LIGHT") == 0)
                {
                    char send[100] = "LIGHTRequest=";
                    strcat(send, pmoduleparam->valuestring);
                    /* 发送串口指令 */
                    uart_write_bytes(uart_log_port, send, strlen(send));
                    oled_http_request(send);
                }

                /* 处理区域 */
                xTaskCreate(uart_event_task, "uart_event_task", 2048, NULL, 12, NULL);

                /* 串口5秒不响应,返回400 */
                int countDown = 1000;
                while (countDown > 0 && !state)
                {
                    for (int i = 0; i < 5; i++)
                    {
                        os_delay_us(1000);
                    }
                    countDown--;
                }

                state = 0;

                if (countDown <= 0)
                {
                    /* 超时处理 */
                    cJSON_AddStringToObject(pReturninfo, "result", "Error ! time out");
                    sendPostResponse(false, sendData, pResult);
                    oled_http_no_answer();
                    cJSON_Delete(pRoot);
                    return;
                }
                else
                {
                    /* 响应处理 */
                    cJSON_AddStringToObject(pReturninfo, "result", (const char *)msg);
                    sendPostResponse(true, sendData, pResult);
                    oled_http_success();
                    cJSON_Delete(pRoot);
                    return;
                }
                /* 处理结束 */
            }
            else
            {
                cJSON_AddStringToObject(pResult, "info", "User name or Password incorrect");
            }

            sendPostResponse(true, sendData, pResult);
        }
        else
        {
            cJSON_AddStringToObject(pResult, "info", "No module Item");
            sendPostResponse(false, sendData, pResult);
            cJSON_Delete(pRoot);
            return;
        }
    }
    else
    {
        cJSON_AddStringToObject(pResult, "info", "No Request Item");
        sendPostResponse(false, sendData, pResult);
    }

    cJSON_Delete(pRoot);
}
